<?php 
	
	session_start(); //初始化session

	$chars = join("",array_merge(range('a','z'),range('A', 'Z'),range(0,9) ));
	$chars = str_shuffle($chars);
	$chars = substr($chars, 0, 5); 

	$code = strtolower($chars);  //存放验证码
	$_SESSION['code'] = $code;   //保存session信息

	function showCode($char){
		header("Content-type:image/png");
		$img = imagecreatetruecolor(150, 35); //创建一个画布
		$back = imagecolorallocate($img, 204, 204, 204);
		$black = imagecolorallocate($img, 51, 51, 51);
		$fontColor = imagecolorallocate($img, mt_rand(0,100), mt_rand(0,100), mt_rand(0,100));
		//$fontColor = imagecolorallocate($img,0, 0, 0);
		imagefill($img,0,0,$back);

		$font_file ="Inconsolata.otf";

		$charsArr = str_split($char);
		//imagestring($img, 5, mt_rand(1,20), mt_rand(10,20), $charsArr[0], $fontColor);
		imagefttext($img, 18, mt_rand(0,90), mt_rand(10,25), mt_rand(10,30), $fontColor, $font_file, $charsArr[0]);
		imagefttext($img, 18, mt_rand(0,90), mt_rand(35,50), mt_rand(10,30), $fontColor, $font_file, $charsArr[1]);
		imagefttext($img, 18, mt_rand(0,90), mt_rand(65,80), mt_rand(10,30), $fontColor, $font_file, $charsArr[2]);
		imagefttext($img, 18, mt_rand(0,90), mt_rand(95,110), mt_rand(10,30), $fontColor, $font_file, $charsArr[3]);
		imagefttext($img, 18, mt_rand(0,90), mt_rand(125,140), mt_rand(10,30), $fontColor, $font_file, $charsArr[4]);

		//画干扰点
		for($i=0;$i<50;$i++){
			imagesetpixel($img, mt_rand(1,149), mt_rand(1,34), $black);
			//imagesetpixel：画一个单一像素
		}
		//画干扰线
		for($i=0;$i<5;$i++){
			imageline($img, mt_rand(1,149), mt_rand(1,34), mt_rand(1,149), mt_rand(1,34), $black);
		}

		imagepng($img);
		imagedestroy($img);
	}
	showCode($chars);

	
?>